package com.kz.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.kz.model.Camera;




public class Lightservice {
	
 private DBOpenHelper dbOpenHelper;
 
 public Lightservice(Context context)
 {
	 this.dbOpenHelper = new DBOpenHelper(context);
	 
 }
	
	
	public void save(Camera c){
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.execSQL("insert into redlightscamera (addr1,addr2) values(?,?)",new Object[]{c.getRoadname1(),c.getRoadname2()});
		
	}
	public void update(Camera l){
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.execSQL("update redlightscamera set addr1=?,addr2=?,direction=? where _id=?",new Object[]{l.getRoadname1(),l.getRoadname2(),l.getDirection()});
		
	}
	
     public void delete(Integer id){
    	SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
 		db.execSQL("delete from redlightscamera where _id=?",new Object[]{id.toString()});
		}
     
     
    public Cursor getalllights(){
 		
    	SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		Cursor cursor=db.query("redlightscamera", new String[]{"_id,addr1,addr2"}, null, null, null, null, null);
		return cursor;
 	}
    
    public Camera find(Integer id){
    	Camera c;
    	SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
    	Cursor cursor =db.rawQuery("select * from redlightscamera where _id =?",new String[]{id.toString()});
    	if(cursor.moveToNext()){
    		int lid= cursor.getInt(cursor.getColumnIndex("_id"));
    		String addr1 = cursor.getString(cursor.getColumnIndex("addr1"));
     		String addr2 = cursor.getString(cursor.getColumnIndex("addr2"));
     		String direction = cursor.getString(cursor.getColumnIndex("direction"));
     		
     		c = new Camera(addr1,addr2,direction);
     		return c;	
    	}
    	
    	return null;
 	}
    
    
	public List<Camera> getScrollData(Integer offset, Integer maxResult){
		List<Camera> cameras = new ArrayList<Camera>();
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
	
	
		Cursor cursor = db.query("redlightscamera", null, null, null, null, null, null, offset+","+ maxResult);
		
		while(cursor.moveToNext()){
			int id = cursor.getInt(cursor.getColumnIndex("_id"));
			String addr1 = cursor.getString(cursor.getColumnIndex("addr1"));
			String addr2 = cursor.getString(cursor.getColumnIndex("addr2"));
			String direction = cursor.getString(cursor.getColumnIndex("direction"));
			Camera camera;
			if("".equals(direction))
			{
			camera = new Camera(id,addr1, addr2);
			}
			else
			{
			 camera = new Camera(id,addr1, addr2,direction);
			}
			
			cameras.add(camera);
		}
		cursor.close();
		return cameras;
	}
	

    
    
    
    public long getCount(){
    	SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
    	Cursor cursor =db.rawQuery("select count(_id) from redlightscamera",null);
    	if(cursor.moveToNext())
    		{
    		  return cursor.getLong(0);
    		}
    		return 0;
    	}
    
}
